<?php

if(!defined('DIR_VSPAYMENT'))
	define(dirname(__FILE__).DIR_SEP);

require_once($curDir."vspayment.php");

require_once(DIR_VSPAYMENT.'templates'.DIR_SEP.'creditcard_form.php');

class VsPaymentCreditCard extends VsPayment {
	protected $_sqlTable = 'payment_credit_card';

	protected $_cardOwner = '';
	protected $_cardType = '';
	protected $_cardNumber = '';
	protected $_cardExpiryMonth = '';
	protected $_cardExpiryYear = '';
	
	function __construct() {
		$this->_form = &new VsPaymentFormCreditCard();
	}
	//=================================================================================================

	function cardOwner() { return $this->_cardOwner; }
	function cardType() { return $this->_cardType; }
	function cardNumber() { return $this->_cardNumber; }
	function cardExpiryMonth() { return $this->_cardExpiryMonth; }
	function cardExpiryYear() { return $this->_cardExpiryYear; }
	//=================================================================================================

	function setCardOwner($owner) { $this->_cardOwner = $owner; }
	function setCardType($type) { $this->_cardType = $type; }
	function setCardNumber($number) { $this->_cardNumber = $number; }
	function setCardExpiryMonth($month) { $this->_cardExpiryMonth = $month; }
	function setCardExpiryYear($year) { $this->_cardExpiryYear = $year; }
	//=================================================================================================

	private function findType($cardNumber) {


	}
	//================================================================================================



	// Sledva6tite dve funkcii sa ot OsCommerce "includes\classes\cc_validation.php"
	function validate($number, $expiry_m, $expiry_y) {
		$this->cc_number = ereg_replace('[^0-9]', '', $number);

		if (ereg('^4[0-9]{12}([0-9]{3})?$', $this->cc_number)) {
			$this->cc_type = 'Visa';
		} elseif (ereg('^5[1-5][0-9]{14}$', $this->cc_number)) {
			$this->cc_type = 'Master Card';
		} elseif (ereg('^3[47][0-9]{13}$', $this->cc_number)) {
			$this->cc_type = 'American Express';
		} elseif (ereg('^3(0[0-5]|[68][0-9])[0-9]{11}$', $this->cc_number)) {
			$this->cc_type = 'Diners Club';
		} elseif (ereg('^6011[0-9]{12}$', $this->cc_number)) {
			$this->cc_type = 'Discover';
		} elseif (ereg('^(3[0-9]{4}|2131|1800)[0-9]{11}$', $this->cc_number)) {
			$this->cc_type = 'JCB';
		} elseif (ereg('^5610[0-9]{12}$', $this->cc_number)) { 
			$this->cc_type = 'Australian BankCard';
		} else {
			return -1; // Unknown Card Type
		}

		if (is_numeric($expiry_m) && ($expiry_m > 0) && ($expiry_m < 13)) {
			$this->cc_expiry_month = $expiry_m;
		} else {
			return -2;
		}

		$current_year = date('Y');
		$expiry_y = substr($current_year, 0, 2) . $expiry_y;
		if (is_numeric($expiry_y) && ($expiry_y >= $current_year) && ($expiry_y <= ($current_year + 10))) {
			$this->cc_expiry_year = $expiry_y;
		} else {
			return -3;
		}

		if ($expiry_y == $current_year) {
			if ($expiry_m < date('n')) {
				return -4;
			}
		}

		return $this->is_valid();
	}
	//=======================================================================================================================

    function is_valid() {
		$cardNumber = strrev($this->cc_number);
		$numSum = 0;

		for ($i=0; $i<strlen($cardNumber); $i++) {
			$currentNum = substr($cardNumber, $i, 1);

			// Double every second digit
			if ($i % 2 == 1) {
				$currentNum *= 2;
			}

			// Add digits of 2-digit numbers together
			if ($currentNum > 9) {
				$firstNum = $currentNum % 10;
				$secondNum = ($currentNum - $firstNum) / 10;
				$currentNum = $firstNum + $secondNum;
			}

			$numSum += $currentNum;
		}

		// If the total has no remainder it's OK
		return ($numSum % 10 == 0);
	}
	//=================================================================================================================================

};

?>
